#include "PlanetToolbar.hh"

using namespace Gui;
using namespace Network;

PlanetToolbar::PlanetToolbar(QWidget *parent):
    QWidget(parent),
    starId(),
    selectedPlanet(0)
{
	ui.setupUi(this);
}

PlanetToolbar::~PlanetToolbar() {}

void PlanetToolbar::clear() {
    selectedPlanet = 0;
}

void PlanetToolbar::showStar(Csillag* star) {
    starId = star->get_id();
	QLabel* starname = findChild< QLabel* >("starName");
	starname->setText(QString::number(star->get_id(),10));

	{
		QList<Bolygo*> oldlist =  findChildren<Bolygo*>();
		QList<Bolygo*>::iterator i;
		for (i = oldlist.begin(); i != oldlist.end(); i++) {
			(*i)->hide();
			delete *i;
		}
	}

	StateChanged::StarInfo::PlanetList pl = star->get_planets();
	QVector< StateChanged::PlanetInfo >::Iterator i;
	int x = 10;
	int y = 50;
	for (i = pl.begin(); i != pl.end(); i++) {
		Bolygo* bolygo = new Bolygo(this, (*i));
		QPoint p();
		bolygo->move(x, y);
		connect(
          bolygo, SIGNAL(left_clicked(Bolygo*)),
          this,   SLOT(planet_selected(Bolygo*))
        );
        connect(
          bolygo, SIGNAL(right_clicked(Bolygo*)),
          this,   SLOT(emit_planet_targeted(Bolygo*))
        );
		bolygo->show();
		y += bolygo->size().height() + 6;
	}
}

void PlanetToolbar::planet_selected(Bolygo* planet) {
    qDebug("slot! PlanetToolbar::planet_selected");
    if (planet == selectedPlanet)
        return;

    if (selectedPlanet)
        selectedPlanet->deselect();

    selectedPlanet = planet;

    if (selectedPlanet) {
        selectedPlanet->select();
        qDebug("signal! PlanetToolbar::planet_selected_sig");
        emit planet_selected_sig(planet);
    }
}

void PlanetToolbar::emit_planet_targeted(Bolygo* planet) {
    qDebug("slot! PlanetToolbar::emit_planet_targeted");
    qDebug("signal! PlanetToolbar::planet_targeted");
    emit planet_targeted(starId, planet);
}
